﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using System.Configuration;
using Ext.Net;
using System.Data;
using Newtonsoft.Json;
using System.IO;
using System.Text;


namespace TCDC_System.Content.MasterData
{
    public partial class p_tcdc_mst_usergroup : SystemPage.UIPage
    {
        private tcdc_mst_usergroup objUsergroup;
        protected void Page_Init(object sender, EventArgs e)
        {
            this.UsergroupAdd1.GridStore = StoreUsergroup;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            objUsergroup = new tcdc_mst_usergroup();
            objUsergroup.strConn = ConfigurationManager.AppSettings["ConnString"];
            if (!X.IsAjaxRequest)
            {
                StoreUsergroup.DataSource = GetUsergroup();
                StoreUsergroup.DataBind();
            }
        }

        protected DataTable GetUsergroup()
        {
            DataTable dt = objUsergroup.getByObject(new tcdc_mst_usergroup());
            return dt;
        }

        protected void AddUsergroup(object sender, DirectEventArgs e)
        {
            UsergroupAdd1.Show();
        }

        protected void UpdateUsergroup(object sender, DirectEventArgs e)
        {
            string json = e.ExtraParams["UsergroupValue"];
            Dictionary<string, string>[] workingdays = JSON.Deserialize<Dictionary<string, string>[]>(json);
            string usergroup_id = string.Empty;
            string usergroup_name = string.Empty;
            string contact = string.Empty;
            string usergroup_address = string.Empty;
            string telephone = string.Empty;
            string email = string.Empty;
            string create_date = string.Empty;
            string create_by = string.Empty;
            string update_date = string.Empty;
            string update_by = string.Empty;

            foreach (Dictionary<string, string> row in workingdays)
            {
                usergroup_id = row["usergroup_id"].ToString();
                usergroup_name = row["usergroup_name"].ToString();
                contact = row["contact"].ToString();
                usergroup_address = row["usergroup_address"].ToString();
                telephone = row["telephone"].ToString();
                email = row["email"].ToString();
                create_date = row["create_date"].ToString();
                create_by = row["create_by"].ToString();
                update_date = row["update_date"].ToString();
                update_by = row["update_by"].ToString();
            }

            objUsergroup.usergroup_id = Convert.ToInt32(usergroup_id);
            objUsergroup.usergroup_name = usergroup_name;
            objUsergroup.contact = contact;
            objUsergroup.usergroup_address = usergroup_address;
            objUsergroup.telephone = telephone;
            objUsergroup.email = email;
            objUsergroup.create_date = create_date;
            objUsergroup.create_by = Convert.ToInt32(create_by);
            objUsergroup.update_date = update_date;
            objUsergroup.update_by = Convert.ToInt32(update_by);
            objUsergroup.updateData(objUsergroup);
            StoreUsergroup.DataSource = GetUsergroup();
            StoreUsergroup.DataBind();
        }

        protected void DeleteUsergroup(object sender, DirectEventArgs e)
        {
            this.ConfirmDeleteUsergroup();
        }

        protected void StoreUsergroup_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            StoreUsergroup.DataSource = GetUsergroup();
            StoreUsergroup.DataBind();
        }

        protected void ExportExcelUsergroup(object sender, EventArgs e)
        {
            string json = GridData.Value.ToString();

            DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));

            if (dt.Rows.Count > 0)
            {
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename=Usergroup.xls");
                Response.ContentType = "application/vnd.ms-excel";
                StringWriter stringWriter = new StringWriter();
                HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
                DataGrid dataExportExcel = new DataGrid();
                dataExportExcel.DataSource = dt;
                dataExportExcel.DataBind();
                dataExportExcel.RenderControl(htmlWrite);
                StringBuilder sbResponseString = new StringBuilder();
                sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head> <body>");
                sbResponseString.Append(stringWriter + "</body></html>");
                sbResponseString.Replace("&nbsp;", "&#160;");
                Response.Write(sbResponseString.ToString());
                Response.End();
            }
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void ConfirmDeleteUsergroup()
        {
            X.Msg.Confirm("Confirm", "ยืนยันการลบ ?", new MessageBoxButtonsConfig
            {
                Yes = new MessageBoxButtonConfig
                {
                    Handler = "MyNameSpace.DeleteDataUsergroup()",
                    Text = "Yes"
                },
                No = new MessageBoxButtonConfig { Text = "No" }
            }).Show();
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void DeleteDataUsergroup()
        {
            RowSelectionModel sm = this.GridPanelUsergroup.SelectionModel.Primary as RowSelectionModel;
            string usergroup_id = string.Empty;

            foreach (SelectedRow row in sm.SelectedRows)
            {
                usergroup_id = row.RecordID;
            }
            objUsergroup.deleteData(usergroup_id);
            StoreUsergroup.DataSource = GetUsergroup();
            StoreUsergroup.DataBind();
        }
    }
}

